/**
 * 
 */
package com.base.myproject.client.busi.b2c.analysis;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.event.ChartEvent;
import com.extjs.gxt.charts.client.event.ChartListener;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Slider;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * 热销商品分析，显示某个时间段，指定部门的，销售量排名前几位的商品。
 * @author zgw
 *2009-8-24 下午09:35:08
 * javazgw@gmail.com
 */
public class HotSaleAnalysis extends ContentPanel{
	 BaseStore<JDBCModelData> store;
	 int showcount = 10; //  显示个数
	 int maxshowcount = 40; //最大显示个数
	 ChartListener listener;
	public HotSaleAnalysis()
	{
		init();
	}
	 void init()
	 {
		 setWidth("720px");
		 initstore();
		 initlistener();
		 initwidget();
		// initSlider();
		
	 }
	 void initstore()
	 {

			ModelType type = new ModelType();
			type.setRoot("records");
			type.setRecordName("record");

			type.addField("b2cgcode");
			type.addField("b2cgname");
		//	type.addField("orderdate");
			type.addField("qty");
		//	type.addField("finalprice");
		//	type.addField("totle");

			StoreInfo info = new StoreInfo();

			info.setTablename("b2clistorderg");
			//info.setKeyname(new String[] { "b2cgcode" });
			info.setSql("select top "+showcount+" (select b2cgname from b2cgcode where b2cgcode.b2cgcode = b2clistorderg.b2cgcode) as b2cgname,b2cgcode,sum(cast(qty as int)) as qty from b2clistorderg where qty is not null or b2cgname is not null group by b2cgcode order by qty  desc");
			store = new BaseStore<JDBCModelData>(type, info);
	 }
	 void initwidget()
	 {
		 ChartModel model = new ChartModel("热销商品销售量柱状图",  
	        "font-size: 16px; font-family: Verdana; text-align: center;");  
		  model.setBackgroundColour("#fefefe");  
		    model.setLegend(new Legend(Position.TOP, true));  
		    model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);  
		    
		    BarChart bar = new BarChart(BarStyle.GLASS);  
		    bar.setColour("#00aaff");  
		    BarDataProvider barProvider = new BarDataProvider( "qty","b2cgname");  
		    barProvider.bind(store);  
		    bar.setDataProvider(barProvider);  
		   bar.addChartListener(listener);  
		    model.addChartConfig(bar);  
		    
		    
		    String url = "chart/open-flash-chart.swf";
		    final Chart chart = new Chart(url);  
		    chart.setToolTip("点击产品的图型看看");
		    chart.setChartModel(model);  
		    chart.setHeight("300px");
		    add(chart);
	 }
	 void initlistener()
	 {
		 listener = new ChartListener() {  
		      public void chartClick(ChartEvent ce) {  
		        int row = ce.getChartConfig().getValues().indexOf(ce.getDataType());  
		        int col = ce.getChartModel().getChartConfigs().indexOf(ce.getChartConfig()) + 1;  
		       // CellSelectionModel<TeamSales> csm = (CellSelectionModel<TeamSales>) teamSalesGrid.getSelectionModel();  
		       /* if (selRadio.getValue()) {  
		        //  csm.selectCell(row, col);  
		        } else {  
		        //  teamSalesGrid.startEditing(row, col);  
		        }  */
		     //   System.out.println(ce);
		        System.out.println(((BarDataProvider)ce.getChartConfig().getDataProvider()));
		        System.out.println(((BarDataProvider)ce.getChartConfig().getDataProvider()).getLabelProperty());
		        System.out.println(store.getValue(row, "b2cgname"));
		        System.out.println(store.getValue(row, "b2cgcode"));
		        String b2cgcode = store.getValue(row, "b2cgcode")+"";
		        String b2cgname = store.getValue(row, "b2cgname")+"";
		        OneProSaleAnalysis one = new OneProSaleAnalysis(b2cgcode,b2cgname);
		        RootPanel.get("zgwtest").add(one);
		      }  
		    };  
	 }
	 void initSlider()
	 {
		 final Slider slider = new Slider();  
		    slider.setWidth(200);  
		   // slider.setIncrement(1);  
		    slider.setMessage("{0}% complete");  
		    slider.setMaxValue(maxshowcount);  
		    slider.setClickToChange(true);  
		    slider.addListener(Events.Change, new Listener()
		    {

				@Override
				public void handleEvent(BaseEvent be) {
					// TODO Auto-generated method stub
					System.out.println("---");
				}
		    	
		    });
		    add(slider);
	 }
	 
	/**
	 * @return the store
	 */
	public BaseStore<JDBCModelData> getStore() {
		return store;
	}
	/**
	 * @param store the store to set
	 */
	public void setStore(BaseStore<JDBCModelData> store) {
		this.store = store;
	}
	 
}
